package com.sanploy.file.util;


import net.coobird.thumbnailator.Thumbnails;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

/**
 *  Thumbnails 图片处理工具类 功能 等比例缩放 裁切 加水印 复合操作等
 *
 * @author 13643
 *
 */
public class ImageUtil {
    /**
     * 按原来的比例缩放
     * 若图片横比width小，高比height小，不变 若图片横比width小，高比height大，高缩小到height，图片比例不变
     * 若图片横比width大，高比height小，横缩小到width，图片比例不变
     * 若图片横比width大，高比height大，图片按比例缩小，横为width或高为height
     * @throws IOException
     */
    public static String changeImgSizeRatio(String src,String target,int width,int height) throws IOException {
        File targetFile = new File(FileUtil.fold,target);
        FileUtil.pathIsExist(targetFile);
        Thumbnails.of(new File(FileUtil.fold,src)).size(width, height).toFile(targetFile);
        return (File.separator+target).replaceAll(File.separator+File.separator,"/");
    }
    /**
     * 按指定尺寸强制缩放
     * @throws IOException
     */
    public static String changeImgSize(String src,String target,int width,int height){
        try {
            File targetFile = new File(FileUtil.fold,target);
            FileUtil.pathIsExist(targetFile);
            Thumbnails.of(new File(FileUtil.fold,src)).size(width, height).keepAspectRatio(false).toFile(targetFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (File.separator+target).replaceAll(File.separator+File.separator,"/");
    }

    public static boolean isPicture(String pictureName){
       
        String imgeArray[] = {".bmp", ".dib", ".gif", ".jfif", ".jpe", ".jpeg", ".jpg", ".png", ".tif", ".tiff", ".ico", ".10"};
        for (String imgFormat:imgeArray){
            if (pictureName.contains(imgFormat)){
                return true;
            }
        }
        return false;
    }
}  